package com.irun.pm.ui;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageButton;

import com.irun.pm.R;
import com.irun.pm.utils.StringUtil;
import com.irun.pm.utils.Utils;
import com.irun.pm.view.ViewScroll;

public class PhotoView extends Activity implements OnClickListener{

	private static final String TAG = "PhotoView";

	private ViewScroll viewScroll;

	private ImageButton zoomIn;
	private ImageButton zoomOut;
	private ImageButton rotateLeft;
	private ImageButton rotateRight;
	private Bitmap bitmapImage;
	private Bitmap bitmapMaxImage;
	private String imgPath = "";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.layout_view_photo);
		viewScroll = (ViewScroll) findViewById(R.id.iv_photo);
		zoomIn = (ImageButton)findViewById(R.id.btn_zoom_in);
		zoomOut = (ImageButton)findViewById(R.id.btn_zoom_out);
		rotateLeft = (ImageButton)findViewById(R.id.btn_retate_anticlockwise);
		rotateRight = (ImageButton)findViewById(R.id.btn_retate_clockwise);
		zoomIn.setOnClickListener(this);
		zoomOut.setOnClickListener(this);
		rotateLeft.setOnClickListener(this);
		rotateRight.setOnClickListener(this);
		imgPath  = getIntent().getStringExtra("path");
		Log.i(TAG, "imgPath="+imgPath);
		try{
			bitmapImage = Utils.getBitmapByPath(imgPath, 200, 200);//BitmapFactory.decodeFile(imgPath); // 此时返回bm为空
		}catch (OutOfMemoryError e) {
			e.printStackTrace();
			bitmapImage = null;
		}
		if(bitmapImage!= null){
			viewScroll.start(this, bitmapImage);
		}else{
			Intent intent = new Intent();
			setResult(RESULT_OK,intent );
			finish();
		}
		new Thread(){
			public void run(){
				Log.i(TAG, "imgPath = "+imgPath);
				PhotoView.this.runOnUiThread(new Runnable() {
					
					@Override
					public void run() {
						if(!StringUtil.isEmpty(imgPath)){
							try{
								bitmapMaxImage = BitmapFactory.decodeFile(imgPath); // 此时返回bm为空
							}catch (OutOfMemoryError e) {
								e.printStackTrace();
								bitmapMaxImage = null;
							}
							if(bitmapMaxImage!= null){
								viewScroll.start(PhotoView.this, bitmapMaxImage);
							}
						}
					}
				});
				
			}
		}.start();
		
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		//放大
		case R.id.btn_zoom_in:
			viewScroll.zoomImg(true);
			break;
		//缩小
		case R.id.btn_zoom_out:
			System.out.println("缩小");
			viewScroll.zoomImg(false);
			break;
		//左转90度
		case R.id.btn_retate_anticlockwise:
			viewScroll.rotate(true);
			break;
		//右转90度
		case R.id.btn_retate_clockwise:
			viewScroll.rotate(false);
			break;
		default:
			break;
		}
		
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		if(bitmapImage!=null){
			bitmapImage.recycle();
		}
	}
	

}
